package com.tran.app.screen

import android.text.TextUtils
import android.util.Log
import android.widget.RadioGroup
import androidx.compose.foundation.Image
import androidx.compose.foundation.background
import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.size
import androidx.compose.foundation.layout.width
import androidx.compose.foundation.layout.wrapContentHeight
import androidx.compose.foundation.layout.wrapContentSize
import androidx.compose.foundation.shape.CircleShape
import androidx.compose.material3.Button
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.OutlinedTextField
import androidx.compose.material3.RadioButton
import androidx.compose.material3.RadioButtonDefaults
import androidx.compose.material3.Text
import androidx.compose.material3.TextField
import androidx.compose.material3.TextFieldDefaults
import androidx.compose.runtime.Composable
import androidx.compose.runtime.collectAsState
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.rememberCoroutineScope
import androidx.compose.runtime.setValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.clip
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.layout.ContentScale
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.text.style.TextAlign
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import androidx.lifecycle.ViewModel
import androidx.navigation.NavController
import com.orhanobut.logger.Logger
import com.tran.app.MyApp
import com.tran.app.R
import com.tran.app.dao.AppDatabase
import com.tran.app.utils.show
import com.tran.app.viewmodel.LoginViewModel
import com.tran.app.viewmodel.MainViewModel
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext

@Composable
fun Login(modifier: Modifier,navController:NavController,vm:LoginViewModel){
    val uistate by vm.uidata.collectAsState()
    Logger.d(uistate.toString())
    Column(modifier = modifier.fillMaxSize(),
        horizontalAlignment = Alignment.CenterHorizontally
   ) {
        Image(
            painter = painterResource(id = R.drawable.img), contentDescription = "",
            Modifier
                .padding(top = 120.dp)
                .clip(
                    CircleShape
                )
                .size(120.dp), contentScale = ContentScale.Crop
        )
        Text(text = LocalContext.current.getString(R.string.app_name))
        OutlinedTextField(modifier = Modifier.padding(top = 10.dp),
            maxLines = 1,label = {
            Text(text = "用户名")
        }, value = uistate.name, onValueChange = {
            vm.setui(uistate.copy(name = it))
        })
        OutlinedTextField(modifier = Modifier.padding(10.dp),
             maxLines = 1, label = {
            Text(text = "密码")
        }, value = uistate.pw, onValueChange = { it ->
            vm.setui( uistate.copy(pw = it))
        })
        MyRadioGroup(vm)
        Button(modifier = Modifier
            .padding(top = 10.dp)
            .width(200.dp),onClick = {
            navController.navigate("register")
        }) {
            Text(text = "注册用户")
        }
        Button(modifier = Modifier
            .padding(top = 10.dp)
            .width(200.dp),onClick = {
                    vm.login(navController)
        }) {
            Text(text = "登录")
        }
    }
}

@Composable
fun MyRadioGroup(vm: LoginViewModel) {
    val options = listOf("老师", "学生")
    val select = vm.uidata.collectAsState()
    Row(modifier = Modifier
        .fillMaxWidth()
        .wrapContentHeight(),
        horizontalArrangement = Arrangement.Center, // 使用SpaceEvenly使RadioButton均匀分布
        verticalAlignment = Alignment.CenterVertically) {
        options.forEach { option ->
                RadioButton(
                    selected = (select.value.work == option),
                    onClick = {
                        vm.setui(select.value.copy(work = option))
                    },
                    colors = RadioButtonDefaults.colors()
                )
                Text(option, textAlign = TextAlign.Center)
        }
    }
}